package com.wxm.killing.mq.controller;

import com.wxm.killing.mq.common.auth.UserUtil;
import com.wxm.killing.mq.common.datasource.IPKeyGenerator;
import com.wxm.killing.mq.common.model.JsonResult;
import com.wxm.killing.mq.common.util.CommonUtil;
import com.wxm.killing.mq.common.util.JsonUtil;
import com.wxm.killing.mq.dao.TbContractRushStatusDao;
import com.wxm.killing.mq.entity.RushBuyMsg;
import com.wxm.killing.mq.entity.TbContractRushStatus;
import com.wxm.killing.mq.producer.JMSProducer;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description 抢购业务
 * @author: wxm
 * @date: 2019/10/27 13:09
 * @version: 1.0
 */
@Api(tags = "抢购业务", value = "抢购业务")
@RestController
@RequestMapping(value = "/rushBuy")
public class RushBuyController {

    private final static Logger logger = LoggerFactory.getLogger(RushBuyController.class);
    @Autowired
    private IPKeyGenerator ipKeyGenerator;

    @Autowired
    private TbContractRushStatusDao tbContractRushStatusDao;

    @Autowired
    private JMSProducer jMSProducer;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 抢购
     *
     * @param map
     * @return
     */
    @PostMapping("/rushBuy")
    @Transactional()
    public JsonResult rushBuy(@RequestParam Map<String, Object> map) {
        Long startTime = System.currentTimeMillis();
        logger.info("RushBuyController.rushBuy start：" + JsonUtil.Java2Json(map));
        Integer contractId = CommonUtil.getInteger(map, "contract_id");
        JsonResult jsonResult = new JsonResult();
        jsonResult.setStatus(0);
        if (contractId == null) {
            logger.info("系统错误");
            jsonResult.setMsg("系统错误");
            return jsonResult;
        }

        //判断是否已有排队中
        List<Integer> sqlBuyTodayParam = new ArrayList<Integer>();
        sqlBuyTodayParam.add(UserUtil.getUserId());
        sqlBuyTodayParam.add(contractId);
        String sqlCountRush = " SELECT count(*) from tb_contract_rush_status where  status=0 AND   user_id=? and contract_id=? AND  to_days(create_time) = to_days(now())";
        int countRush = jdbcTemplate.queryForObject(sqlCountRush, sqlBuyTodayParam.toArray(), Integer.class);
        if (countRush > 0) {
            logger.info("已有排队抢购订单");
            jsonResult.setMsg("已有排队抢购订单");
            return jsonResult;
        }

        //抢购状态入库
        String contract_rush_buy_status_id = ipKeyGenerator.getGenerateKey();
        TbContractRushStatus tbContractRushStatus = new TbContractRushStatus();
        tbContractRushStatus.setContractRushBuyStatusId(contract_rush_buy_status_id);
        tbContractRushStatus.setStatus(0);
        tbContractRushStatus.setContractId(contractId);
        tbContractRushStatus.setUserId(UserUtil.getUserId());
        tbContractRushStatus.setMsg("进入消息队列");
        tbContractRushStatus.setIp(UserUtil.getIp());
        tbContractRushStatusDao.insertSelective(tbContractRushStatus);

        //抢购进入消息队列,传递用户ID,合约ID
        RushBuyMsg rushBuyMsg = new RushBuyMsg();
        rushBuyMsg.setContractId(contractId);
        rushBuyMsg.setUserId(UserUtil.getUserId());
        rushBuyMsg.setContractRushBuyStatusId(contract_rush_buy_status_id);
        jMSProducer.sendKillingMessage(JsonUtil.Java2Json(rushBuyMsg));
        Long endTime = System.currentTimeMillis();
        Long useTime = endTime - startTime;
        logger.info("RushBuyController.rushBuy end:"+useTime);
        //状态ID返回给前端
        jsonResult.setData(contract_rush_buy_status_id);
        jsonResult.setStatus(1);
        return jsonResult;
    }
}
